//
//  WUPictureTableViewCell.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/5/9.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "WUPictureCell.h"
#import "WUDetailViewController.h"
#import <SDCycleScrollView.h>
#import <MWPhotoBrowser.h>

@interface WUPictureCell()<SDCycleScrollViewDelegate, MWPhotoBrowserDelegate>
{
    NSMutableArray *_photos;
    NSMutableArray *_thumbs;

    NSMutableArray *array;

    
    NSMutableArray *myArray;
}
@property (nonatomic,retain)UIButton *button;
@property (nonatomic,retain)UIImageView *myImageView;
@property (nonatomic,retain)SDCycleScrollView *imgView;

@end

@implementation WUPictureCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView{
    
    
    //下一步
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 5;
    [self.contentView addSubview:self.button];
    [self.button setTitle:@"参考图片" forState:UIControlStateNormal];
    //  [self.button addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // [self.button setTitleColor:HS forState:UIControlStateNormal];
    //    self.button.frame = CGRectMake(50.00*AUTO_WIDTH, 600.00*AUTO_WIDTH, 275.00*AUTO_WIDTH, 40.00*AUTO_WIDTH);
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(7 * AUTO_WIDTH);
        make.right.mas_equalTo(-7 * AUTO_WIDTH);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        
    }];
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:TEXTCONTENTFONT2 * TEXTWEIGHT];
    
    
    
    
    _myImageView = [UIImageView new];
    _myImageView.clipsToBounds = YES;
    _myImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    _myImageView.userInteractionEnabled=YES;
    [self.contentView addSubview:_myImageView];
    [_myImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.button.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.contentView.mas_left).offset(7 * AUTO_WIDTH);
        make.right.mas_equalTo(self.contentView.mas_right).offset(- 7 * AUTO_WIDTH);
        make.height.mas_equalTo(210 * AUTO_HEIGHT);
        
    }];
    array = [NSMutableArray array];

    
//    array=@[@"http://121.40.144.148/Uploads/Task/2016-07-11/s_57832f2c6586a.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg"];
    
//    self.imgView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-32 *AUTO_WIDTH, 210 *AUTO_HEIGHT) imageURLStringsGroup:array];
//    self.imgView = [[SDCycleScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth - 32 * AUTO_WIDTH, 210 *AUTO_HEIGHT)];
    self.imgView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth - 32 * AUTO_WIDTH, 210 * AUTO_HEIGHT) delegate:self placeholderImage:nil];
   
    

    self.imgView.clipsToBounds=YES;
    self.imgView.delegate=self;
    self.imgView.userInteractionEnabled=YES;
    _imgView.clipsToBounds = YES;
    _imgView.contentMode = UIViewContentModeScaleAspectFit;
    self.imgView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    
    self.imgView.infiniteLoop=YES;
    self.imgView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    self.imgView.autoScrollTimeInterval = 3;
    [_myImageView addSubview:self.imgView];
    
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"tap tag is %ld", index);
    // Browser
    _photos = [[NSMutableArray alloc] init];
    _thumbs = [[NSMutableArray alloc] init];
    for (int i=0; i<array.count; i++)
    {
//        MWPhoto *photo = [MWPhoto photoWithImage:[UIImage imageNamed:array[i]]];
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",array[i]]]];
        [_photos addObject:photo];
    }
    
    //调用父View的里面的方法
    WUDetailViewController *vc = (WUDetailViewController *)self.superview.superview.nextResponder;
       NSLog(@"%@", self.superview.superview.nextResponder);
    // Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    vc.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    browser.displayActionButton = NO;//分享按钮,默认是
    browser.displayNavArrows = YES;//左右分页切换,默认否
    browser.displaySelectionButtons = NO;//是否显示选择按钮在图片上,默认否
    browser.alwaysShowControls = NO;//控制条件控件 是否显示,默认否
    browser.zoomPhotosToFill = YES;//是否全屏,默认是
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;//是否全屏
#endif
    browser.enableGrid = YES;//是否允许用网格查看所有图片,默认是
    browser.startOnGrid = NO;//是否第一张,默认否
    browser.enableSwipeToDismiss = YES;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:index];
    
    
    [vc.navigationController pushViewController:browser animated:YES];
}

#pragma mark - MWPhotoBrowserDelegate
-(id<MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index
{
    return _photos[index];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}


- (id )photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

-(void) Back:(UIButton *) btn
{
    WUDetailViewController *vc = (WUDetailViewController *)self.superview.superview.nextResponder;
    [vc.navigationController popViewControllerAnimated:YES];
}
-(void)settingRegisnModel:(ResignModel *)moedl{


    for (NSMutableDictionary *dic in moedl.simg) {
        
        NSString *url_image = [dic objectForKey:@"simg"];
        [array insertObject:[NSString stringWithFormat:@"%@%@",HTTPURL,url_image] atIndex:array.count];
        
        
//        array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTPURL,url_image]]];
//        [_myImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTPURL,url_image]] placeholderImage:nil];
//        array = @[[NSString stringWithFormat:@"%@%@",HTTPURL,url_image]];
    }
    
    self.imgView.imageURLStringsGroup = array;
//    array = myArray;

}
@end
